package com.orange.wwwapi.controller.activity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.activity.dto.ActivityRegistrationOrder;
import com.orange.core.pojo.entity.activity.vo.ActivityFrontVO;
import com.orange.core.pojo.entity.activity.vo.ActivityVO;
import com.orange.core.pojo.entity.activityRegistration.dto.ActivityRegistrationDTO;
import com.orange.core.pojo.entity.activityRegistration.vo.ActivityRegistrationVO;
import com.orange.core.pojo.req.activity.ActivityPageReq;
import com.orange.core.pojo.req.activityRegistrations.ActivityRegistrationPageReq;
import com.orange.core.pojo.rsp.activity.ActivityOrderPayRsp;
import com.orange.core.pojo.rsp.activity.ActivityOrderRsp;
import com.orange.core.service.activity.ActivityService;
import com.orange.core.util.PrincipalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>ClassName:ActivityController</p>
 * <p>Description: 活动管理controller</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/22 23:19</p>
 */

@RequiredArgsConstructor
@RestController
@RequestMapping("/activity")
public class ActivityController {

    private final ActivityService activityService;

    /**
     * 活动管理分页查询
     * @param pageReqDto
     * @return
     */
    @PostMapping("/page")
    public R<Page<ActivityFrontVO>> page(@RequestBody PageReqDto<ActivityPageReq> pageReqDto) {
        Page<ActivityVO> page = activityService.page(pageReqDto);
        List<ActivityVO> records = page.getRecords();
        List<ActivityFrontVO> activityWeiXinVOList = records.stream().map(activityVO -> {
            ActivityFrontVO activityFrontVO = BeanUtil.copyProperties(activityVO, ActivityFrontVO.class);
            return activityFrontVO;
        }).collect(Collectors.toList());
        Page<ActivityFrontVO> weiXinVOPage = new Page<>(page.getPages(),page.getSize(),page.getTotal());
        weiXinVOPage.setRecords(activityWeiXinVOList);
        return R.ok(weiXinVOPage);
    }

    /**
     * 活动管理详情
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R<ActivityFrontVO> info(@PathVariable("id") int id){
        ActivityVO info = activityService.info(id);
        ActivityFrontVO activityFrontVO = BeanUtil.copyProperties(info, ActivityFrontVO.class);
        return R.ok(activityFrontVO);
    }

    /**
     * 报名活动（停用）
     * @param activityRegistrationDTO
     * @return
     */
    @PostMapping("/register")
    public R<Boolean> registerActivity(@RequestBody @Validated ActivityRegistrationDTO activityRegistrationDTO){
        return R.ok(activityService.registerActivity(activityRegistrationDTO));
    }

    /**
     * 获取用户报名列表
     * @param pageReqDto
     * @return
     */
    @PostMapping("/activityRegistration")
    public R<Page<ActivityRegistrationVO>> getUserRegistrations(@RequestBody PageReqDto<ActivityRegistrationPageReq> pageReqDto){
        Integer userId = PrincipalUtil.getId();
        Assert.notNull(userId, "未获取到用户id");
        ActivityRegistrationPageReq condition = pageReqDto.getCondition();
        condition.setUserId(userId);
        pageReqDto.setCondition(condition);
        return R.ok(activityService.getUserRegistrations(pageReqDto));
    }

    /**
     * 取消报名
     * @param registrationId
     * @param userId
     * @return
     */
    @PostMapping("/cancelRegistration/")
    public R<Boolean> cancelRegistration(Integer registrationId, Integer userId){
        return R.ok(activityService.cancelRegistration(registrationId, userId));
    }

    /**
     * 获取所有的热门活动列表
     * @return
     */
    @GetMapping("/hotActivityList")
    public R<List<ActivityVO>> getHotActivityList(){
        return R.ok(activityService.getHotActivityList());
    }

    /**
     * 创建活动报名订单
     * @param activityId
     * @return
     */
    @PostMapping("/create/{activityId}")
    public R<ActivityOrderRsp> generateOrder(@PathVariable Integer activityId){
        return R.ok(activityService.generateOrder(activityId));
    }


    /**
     * 支付活动报名订单
     * @param activityRegistrationOrder
     * @return
     */
    @PostMapping("/pay")
    public R<ActivityOrderPayRsp> pay(@RequestBody @Validated ActivityRegistrationOrder activityRegistrationOrder){
        return R.ok(activityService.pay(activityRegistrationOrder));
    }

    /**
     * 活动退款
     * @param registrationId 报名名ID
     * @return
     */
    /*@PostMapping("/refund/{registrationId}")
    public R<Boolean> refund(@PathVariable int registrationId){
        return R.ok(activityService.refund(registrationId,null,null,null));
    }*/
}
